<template>
  <div class="app-container">
    <el-tabs
      v-model="activeTabName"
      type="card"
      class="demo-tabs"
      @tab-click="handleClick"
    >
      <el-tab-pane label="待处理的任务" name="1"></el-tab-pane>
      <el-tab-pane label="已处理的任务" name="2"></el-tab-pane>
    </el-tabs>
    <!--搜索栏-->
    <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
      <el-form-item label="关键字搜索" label-width="85" prop="keyValue">
        <el-input
          v-model="queryParams.keyValue"
          placeholder="请输入搜索关键字"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="隐患类型:" label-width="100">
        <el-select
          v-model="queryParams.riskLevelId"
          placeholder="请选择隐患类型"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dangerTypeOptions"
            :key="dict.hazardDangerTypeId"
            :label="dict.hazardDangerTypeName"
            :value="dict.hazardDangerTypeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="隐患来源:" label-width="100">
        <el-select
          v-model="queryParams.accidentTypeId"
          placeholder="请选择"
          multiple
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in hiddenCheckListTypeOptions"
            :key="dict.hiddenListTypeId"
            :label="dict.hiddenListTypeName"
            :value="dict.hiddenListTypeId"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--数据列表-->
    <el-table v-loading="loading" :data="dataList">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column
        label="隐患名称"
        prop="dangerName"
        width="150"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="登记人"
        prop="registrant"
        width="150"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="登记时间" prop="registTime">
        <template #default="scope">
          <span>{{ parseTime(scope.row.registTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患等级" prop="dangerType" :show-overflow-tooltip="true">
        <template #default="scope">
          <span>{{ parseShowDangerType(scope.row.dangerType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患来源" prop="dangerSrc">
        <template #default="scope">
          <span>{{ parseShowDangerSrc(scope.row.dangerSrc) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患类型" prop="height" width="160">
        <template #default="scope">
          <span>{{ parseShowDangerTypeName(scope.row.hazardDangerType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="隐患状态" prop="height">
        <template #default="scope">
          <span>{{ parseShowDangerState(scope.row.dangerState) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip
            v-if="activeTabName == '1'"
            content="去处理整改任务"
            placement="top"
          >
            <el-button link type="primary" plain @click="handleRectify(scope.row)"
              >去处理</el-button
            >
          </el-tooltip>

          <el-tooltip v-else content="详情" placement="top">
            <el-button
              link
              type="primary"
              icon="CircleCheck"
              @click="handleInfo(scope.row)"
              v-hasPermi="['risk:event:query']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.current"
      v-model:limit="queryParams.size"
      @pagination="getList"
    />
  </div>
</template>
<script setup name="MyRectifyTask">
import {
  listMyRectifyTask,
  listMyHandleRectifyTask,
} from "@/api/prevent/workbench/myRectify";
import { listCheckListType } from "@/api/prevent/hidden/checkListType";
import { listDangerType } from "@/api/prevent/hidden/dangerType";
import { listHiddenLevel } from "@/api/prevent/hidden/hiddenLevel";
import { listDangerState } from "@/api/prevent/hidden/dangerState";
const dangerTypeOptions = ref([]); //隐患类型选择选
const hiddenCheckListTypeOptions = ref([]); //隐患排查清单类型选择项
const hiddenLevelOptions = ref([]); //隐患等级选择项
const dangerStateOptions = ref([]); //隐患状态选择项
const { proxy } = getCurrentInstance();
const activeTabName = ref("1"); //默认是待处理的任务
const total = ref(0);
const loading = ref(false);
const dataList = ref([]); //数据列表
const router = useRouter();
const data = reactive({
  form: {},
  queryParams: {
    current: 1,
    size: 10,
    keyValue: undefined,
    riskLevelId: undefined,
    riskUnitId: undefined,
    determineMethodType: undefined,
  },
  rules: {},
});
const { queryParams } = toRefs(data);

/** 处理排查任务的切换切换 */
const handleClick = (tab, event) => {
  if (tab.props.name == "1") {
    getList();
  } else {
    getHandleList();
  }
};

/** 获取隐患类型选择项列表 */
async function getDangerTypeList() {
  listDangerType().then((response) => {
    dangerTypeOptions.value = response.data;
  });
}

/** 获取隐患排查清单类型选择项列表 */
async function getHiddenCheckListTypeList() {
  listCheckListType().then((response) => {
    hiddenCheckListTypeOptions.value = response.data;
  });
}

/** 获取隐患等级选择项列表 */
async function getHiddenLevelList() {
  listHiddenLevel().then((response) => {
    hiddenLevelOptions.value = response.data;
  });
}

/** 获取隐患状态选择项列表 */
async function getDangerStateList() {
  listDangerState().then((response) => {
    dangerStateOptions.value = response.data;
  });
}

/** 处理隐患类型呈现数据 */
function parseShowDangerTypeName(id) {
  if (dangerTypeOptions.value.length < 1) return "----";
  var itemTarget = dangerTypeOptions.value.filter((item) => {
    return item.hazardDangerTypeId == id;
  });
  return itemTarget[0].hazardDangerTypeName;
}

/** 处理隐患等级数据呈现 */
function parseShowDangerType(id) {
  if (hiddenLevelOptions.value.length < 1) return "----";
  var itemTarget = hiddenLevelOptions.value.filter((item) => {
    return item.hiddenLevelId == id;
  });
  if (itemTarget.length < 1) {
    return "error";
  }
  return itemTarget[0].hiddenLevelName;
}

/** 处理隐患来源数据呈现 */
function parseShowDangerSrc(id) {
  if (hiddenCheckListTypeOptions.value.length < 1) return "----";
  var itemTarget = hiddenCheckListTypeOptions.value.filter((item) => {
    return item.hiddenListTypeId == id;
  });
  return itemTarget[0].hiddenListTypeName;
}

/** 处理隐患状态数据呈现 */
function parseShowDangerState(id) {
  if (dangerStateOptions.value.length < 1) return "----";
  var itemTarget = dangerStateOptions.value.filter((item) => {
    return item.dangerStateId == parseInt(id);
  });
  return itemTarget[0].dangerStateName;
}

/** 处理搜索 */
function handleQuery() {
  //待处理的任务
  if (activeTabName.value == "1") {
    getList();
  } else {
    getHandleList();
  }
}
/** 重置 */
function resetQuery() {
  queryParams.value = {
    current: 1,
    size: 10,
    keyValue: undefined,
    riskLevelId: undefined,
    riskUnitId: undefined,
    determineMethodType: undefined,
  };
  proxy.resetForm("queryRef");
  getList();
}

/** 处理任务 */
function handleRectify(row) {
  router.push("/workbench/rectify/handle/" + row.processId);
}
/** 详情 */
function handleInfo(row) {
  router.push("/workbench/rectify/info/" + row.processId);
}
/**获取数据列表 */
function getList() {
  loading.value = true;
  listMyRectifyTask(queryParams.value).then((response) => {
    dataList.value = response.data.dataList;
    total.value = response.data.totalNum;
    loading.value = false;
  });
}
/**获取数据列表 */
function getHandleList() {
  loading.value = true;
  listMyHandleRectifyTask(queryParams.value).then((response) => {
    dataList.value = response.data.dataList;
    total.value = response.data.totalNum;
    loading.value = false;
  });
}

getList();
getDangerTypeList();
getHiddenCheckListTypeList();
getHiddenLevelList();
getDangerStateList();
</script>
